package vcp.client.GUI.mvc.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.border.EtchedBorder;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;

import vcp.client.GUI.events.FormSubmitionEvent;
import vcp.client.GUI.mvc.view.addons.TableColumnAdjuster;
import vcp.client.forms.ReportSummaryForm;
import vcp.client.interfaces.listeners.IFormSubmitionListener;
import vcp.client.reports.ReportAbstract;
import vcp.client.utility.Common;
import vcp.enums.ReportType.ReportReadOnlyType;

public class UIInfoRetrieveView extends UIAbstractView
{

	/**
	 * 
	 */
	private static final long serialVersionUID = - 4973851432829446895L;
	private JTable table;
	private JComboBox <ReportReadOnlyType> cbReportsTypes;
	private JButton btnSearch;

	private IFormSubmitionListener <ReportSummaryForm> formSubmitionListener;

	public void setFormSubmitionListener ( IFormSubmitionListener <ReportSummaryForm> formSubmitionListener )
	{
		this.formSubmitionListener = formSubmitionListener;
	}

	public UIInfoRetrieveView ()
	{
		super();

	}

	// ****************************************************************************************************************************
	// Public methods
	// ****************************************************************************************************************************
	/**
	 * fill view with report information
	 * 
	 * @param report
	 *            with requested data
	 */
	public void loadReadOnlyReport ( ReportAbstract report )
	{
		DefaultTableModel dtm = new DefaultTableModel();
		if ( report != null )
		{
			for ( String header : report.getHeaders() )
			{
				dtm.addColumn(header);
			}

			for ( Object[] object : report.getRows() )
			{
				dtm.addRow(object);
			}

			new TableColumnAdjuster(this.table).adjustColumns();
		}

		// replaces old model
		this.table.setModel(dtm);
	}

	// ****************************************************************************************************************************
	// Private methods
	// ****************************************************************************************************************************
	/**
	 * 
	 * @return the chosen from DDLB {@code ReportReadOnlyType}
	 */
	private ReportReadOnlyType getSelectedReportReadOnlyType ()
	{
		return (ReportReadOnlyType) this.cbReportsTypes.getSelectedItem();
	}

	// ****************************************************************************************************************************
	// UIAbstractView methods
	// ****************************************************************************************************************************
	@Override
	protected void setupLayout ()
	{
		this.setPreferredSize(new Dimension(904, 708));
		this.setLayout(new BorderLayout(0, 0));

		JPanel panel = new JPanel();
		this.add(panel, BorderLayout.NORTH);
		panel.setLayout(new BorderLayout(0, 0));

		JPanel panel_4 = new JPanel();
		FlowLayout flowLayout = (FlowLayout) panel_4.getLayout();
		flowLayout.setAlignment(FlowLayout.LEFT);
		panel.add(panel_4, BorderLayout.WEST);

		JLabel lblNewLabel = new JLabel("Information Type:");
		panel_4.add(lblNewLabel);
		lblNewLabel.setFont(new Font("Lucida Grande", Font.PLAIN, 18));

		this.cbReportsTypes = new JComboBox <>();
		panel_4.add(this.cbReportsTypes);
		this.cbReportsTypes.setFont(new Font("Lucida Grande", Font.PLAIN, 16));
		this.cbReportsTypes.setModel(new DefaultComboBoxModel <ReportReadOnlyType>(ReportReadOnlyType.values()));

		this.btnSearch = new JButton(Common.BTN_SEARCH);
		this.btnSearch.setFont(new Font("Lucida Grande", Font.PLAIN, 18));
		this.btnSearch.setActionCommand(Common.BTN_SEARCH);
		panel.add(this.btnSearch, BorderLayout.EAST);

		JPanel panel_1 = new JPanel();
		this.add(panel_1, BorderLayout.WEST);

		JPanel panel_2 = new JPanel();
		this.add(panel_2, BorderLayout.EAST);

		JPanel panel_3 = new JPanel();
		this.add(panel_3, BorderLayout.SOUTH);

		JScrollPane scrollPane = new JScrollPane();
		this.add(scrollPane, BorderLayout.CENTER);

		final DefaultTableCellRenderer renderRight = new DefaultTableCellRenderer();
		renderRight.setHorizontalAlignment(SwingConstants.CENTER);
		this.table = new JTable()
			{
				/**
				 * 
				 */
				private static final long serialVersionUID = 1L;

				@Override
				public boolean isCellEditable ( int row, int column )
				{
					return false;
				}

				@Override
				public void setRowHeight ( int rowHeight )
				{
					super.setRowHeight(45);
				}

				@Override
				public TableCellRenderer getCellRenderer ( int arg0, int arg1 )
				{
					return renderRight;
				}
			};
		this.table.setGridColor(Color.BLACK);
		this.table.setShowHorizontalLines(true);
		this.table.setShowVerticalLines(true);
		this.table.setBorder(new EtchedBorder(EtchedBorder.RAISED));
		this.table.setShowGrid(true);
		this.table.setFont(new Font("Lucida Grande", Font.PLAIN, 18));

		scrollPane.setViewportView(this.table);
	}

	@Override
	protected void setupListeners ()
	{
		this.btnSearch.addActionListener(this);
	}

	// ****************************************************************************************************************************
	// ActionListener methods
	// ****************************************************************************************************************************
	@Override
	public void actionPerformed ( ActionEvent actionEvent )
	{
		if ( this.formSubmitionListener != null )
		{
			this.formSubmitionListener
					.onFormSubmitted(new FormSubmitionEvent <ReportSummaryForm>(this, new ReportSummaryForm(this.getSelectedReportReadOnlyType())));
		}
	}
}
